package fr.web325.dao;

import fr.web325.utils.QueryRunnerUtils;
import fr.web325.bean.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class UserDao {
    public Class<User> userClass = User.class;


    // 查询所有用户列表
    public List<User> getUserList() throws SQLException {
        // 获取数据库连接池对象
        QueryRunner qr = new QueryRunner(QueryRunnerUtils.getDateSource());
        // 查询并返回Notice对象
        return qr.query("select * from user", new BeanListHandler<>(userClass));
    }

    // 根据ID查询用户
    public User getNoticeById(String id) throws SQLException {
        // 获取数据库连接池对象
        QueryRunner qr = new QueryRunner(QueryRunnerUtils.getDateSource());
        // 查询并返回Notice对象
        return qr.query("select * from user where id=?", new BeanHandler<>(userClass), id);
    }

    // 查询库中是否有这个数据
    public User Login(String name,String pwd) throws SQLException {
        // 获取数据库连接池对象
        QueryRunner qr = new QueryRunner(QueryRunnerUtils.getDateSource());
        // 查询并返回Notice对象
        return qr.query("select * from user where username=? and PASSWORD=?", new BeanHandler<>(userClass), name, pwd) ;
    }


    // 用户注册
    public String addUser(User user) throws SQLException {
        // 获取数据库连接池对象
        QueryRunner qr = new QueryRunner(QueryRunnerUtils.getDateSource());
        User us = qr.query("select * from user where username = ?",new BeanHandler<>(userClass), user.getUsername());
        if(us!=null&&us.getUsername().equals(user.getUsername())){
           return "注册失败,用户名已存在！";
        }else{
           qr.update("insert into user(username,PASSWORD,gender,email,telephone,introduce,activeCode,state,registTime) values (?,?,?,?,?,?,?,?,?)", user.getUsername(),user.getPASSWORD(),user.getGender(),user.getEmail(),user.getTelephone(),user.getIntroduce(),user.getActiveCode(),user.getState(),user.getRegistTime());
           return "注册成功";
        }
    }

    // 根据ID修改用户
    public Boolean editUser(User user) throws SQLException {
        // 获取数据库连接池对象
        QueryRunner qr = new QueryRunner(QueryRunnerUtils.getDateSource());
        return qr.update("update user set username=?,PASSWORD=?,gender=?,email=?,telephone=?,introduce=?,activeCode=?,state=?,role=?,registTime=? where id=?",user.getUsername(),user.getPASSWORD(),user.getGender(),user.getEmail(),user.getEmail(),user.getTelephone(),user.getIntroduce(),user, user.getActiveCode(),user.getState(),user.getRole(),user.getRegistTime()) > 0;
    }

    // 根据id删除Notice
    public Boolean removeUserById(String id) throws SQLException {
        // 获取数据库连接池对象
        QueryRunner qr = new QueryRunner(QueryRunnerUtils.getDateSource());
        return qr.update("delete from user where id=?", id) > 0;
    }

    // 根据激活码 修改用户状态
    public Boolean editUserStatus(String code) throws SQLException {
        // 获取数据库连接池对象
        QueryRunner qr = new QueryRunner(QueryRunnerUtils.getDateSource());
        return qr.update("update user set state=1 where activeCode=?",code) > 0;
    }
}
